package evi.com.evi.activity;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.graphics.Color;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.Toast;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

import evi.com.evi.R;
import evi.com.evi.adapter.LeafMessageAdapter;
import evi.com.evi.application.MyApplication;
import evi.com.evi.bean.BusinessVolBean;
import evi.com.evi.bean.LeafMessageBean;
import evi.com.evi.utils.AppUtils;
import evi.com.evi.utils.ConnectUtil;
import evi.com.evi.utils.ContentUtils;
import evi.com.evi.utils.JSONUtils;
import evi.com.evi.utils.SPUtil;
import evi.com.evi.utils.ScreenUtils;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

/**
 * 留言评价
 *
 * @author MY
 */
public class LeafMessageActivity extends Activity implements OnClickListener {
    private static final String TAG = "TAG";
    private ImageView tv_return_back;
    private ListView lv_business;
    private LeafMessageAdapter mAdapter;
    private JSONUtils jsonUtils;

    private List<LeafMessageBean> list;

    private Dialog dialog;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ScreenUtils.setTranStatus(this, false, Color.parseColor("#FF6600"));
        MyApplication.getInstance().addActivity(this);
        setContentView(R.layout.activity_leaf_message);
        initView();
    }

    private void initView() {
        tv_return_back = (ImageView) findViewById(R.id.tv_return_back);
        tv_return_back.setOnClickListener(this);
        lv_business = (ListView) findViewById(R.id.lv_business);

        jsonUtils = new JSONUtils();
        list = new ArrayList<>();

        showInfo();
        mAdapter = new LeafMessageAdapter(LeafMessageActivity.this, list);
        lv_business.setAdapter(mAdapter);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.tv_return_back:
                finish();
                break;
            default:
                break;
        }
    }

    private void showInfo() {
        dialog = AppUtils.getLoadingDialog(this, "提示", "正在连接服务器，请稍后");
        dialog.show();
        OkHttpClient client = new OkHttpClient();
        String token = SPUtil.getString(this, "token");

        Request request = new Request.Builder()
                .addHeader("token", token)
                .url(ContentUtils.queryComment_url)
                .build();

        client.newCall(request).enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        if (ConnectUtil.isConnect(LeafMessageActivity.this)) {
                            toast("网络信号不佳，加载失败");
                        } else {
                            toast("网络未联接，请检查");
                        }
//                        Toast.makeText(LeafMessageActivity.this, "网络信号不佳，加载失败", Toast.LENGTH_SHORT).show();
                        dialog.dismiss();
                    }
                });
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                String jsonData = response.body().string();
                if (jsonUtils.validate(jsonData)) {
                    JSONObject jsonObject = JSON.parseObject(jsonData);
                    if (jsonObject.getBoolean("success")) {
                        JSONArray jsonArray = jsonObject.getJSONObject("data").getJSONArray("rows");
                        for (int i = 0; i < jsonArray.size(); i++) {
                            JSONObject o = jsonArray.getJSONObject(i);
                            list.add(new LeafMessageBean(o.getString("createDate"), o.getString("orderNo"), o.getString("userPhoneNo"), o.getString("content"), o.getString("orderDate")));
                        }
                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                mAdapter.notifyDataSetChanged();
                            }
                        });
                    } else {
                        jsonObject.getString("message");
                    }
                }
                dismiss();
                Log.e(TAG, "联网成功" + jsonData);
            }
        });


    }


    private void toast(final String string) {
        this.runOnUiThread(new Runnable() {
            @Override
            public void run() {
                Toast.makeText(LeafMessageActivity.this, string, Toast.LENGTH_SHORT).show();
            }
        });
    }

    private void dismiss() {
        this.runOnUiThread(new Runnable() {
            @Override
            public void run() {
                dialog.dismiss();
            }
        });
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        MyApplication.getInstance().deleteActivity(this);
    }
}